// pages/my/index.js
const app = getApp();
const config = app.globalData;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        imgurl:'',
        result:null,
        isLogin: false,
        typeName: '',

        showConfirm: false,
        showConfirmMsg: '',
        confirmFunc: null,
    },

    // 绑定设备
    addEquiemnt(){
        if(config.userInfo.phone==null){
            this.goLogin()
            this.setData({
                isLogin:false
            })
        }else{
            wx.navigateTo({
                url: '../../pages/updateEquipment/index?equipment=true',
            })
        }
    },

    // 解绑设备号
    unbound:function(e){
        let equipmentNo = app.globalData.userInfo.equipmentNo;
        let equipmentType = app.globalData.userInfo.equipmentType;
        let that = this;
        this.setData({
            showConfirm: true,
            showConfirmMsg: "是否确定解绑？",
            confirmFunc: function(){
                wx.request({
                    url: config.url+'api/equipment/unbound',
                    method:'POST',
                    data:{
                        phone:config.userInfo.phone,
                        equipmentNo:equipmentNo,
                        equipmentType:equipmentType
                    },
                    header:{
                        'content-type':'application/json'
                    },
                    success:function(res){
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'success',
                            duration: 1000, 
                            success(data) {
                                setTimeout(function () {
                                    that.getEquipmentInfo()
                                }, 1000) //延迟时间
                            }
                        })
                    }
                })
            }
        })
    },

    goLogin(){
        wx.navigateTo({
            url: '../../pages/login/index',
        })
    },
    sureLogout(){
        let that=this
        wx.request({
            url: config.url+'api/account/loginOut',
            method:'POST',
            data:{phone:config.userInfo.phone},
            success(res){
                if(res.data.code === 0){
                config.userInfo={
                    phone:null,
                    equipmentNo:null,
                    equipmentType:null
                }
                wx.setStorageSync('userInfo', config.userInfo)
                that.onLoad()
                wx.navigateTo({
                    url: '../../pages/login/index',
                })
                }
            }
        })
    },
    // 退出登录
    logout() {
        let that = this
        if(config.userInfo.phone != null){
            this.setData({
                showConfirm: true,
                showConfirmMsg: "是否确定退出登录？",
                confirmFunc: function(){
                    that.sureLogout()
                }
            })
        } else {
            this.isLogin()
        }
    },
    // 判断是否登录
    isLogin(){
        let userinfo = wx.getStorageSync('userInfo')
        if (userinfo.phone != null) { 
            this.setData({
                isLogin:true
            })
        }else{
            this.goLogin()
            this.setData({
                isLogin:false
            })
        }
        return
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            imgurl:config.imgurl,
            result:config.userInfo,
            typeName: config.systemEquipmentTypeName
        });
    },

    // 查看历史
    handlerHis(){
        if(config.userInfo.phone == null){
            this.setData({
                isLogin:false
            })
        } else {
            wx.navigateTo({
                url: '../../pages/wristhistory/index',
            })
        }
    },
    // 获取所有设备
    getEquipmentInfo(){
        let that = this;
        config.userInfo.equipmentNo = null;
        config.userInfo.equipmentType = null;
        that.setData({
            equipmentNo: '',
            equipmentType: '',
        });
        wx.request({
            url: config.url + 'api/equipment/get?phone=' + config.userInfo.phone + '&equipmentType=' + config.systemEquipmentType, 
            success(res){
                if(res.data.code === 0) {
                    let info = res.data.data;
                    if (info) {
                        config.userInfo.equipmentNo = info.equipmentNo;
                        config.userInfo.equipmentType = info.equipmentType;
                        that.setData({
                            equipmentNo: info.equipmentNo,
                            equipmentType: info.equipmentType,
                        });
                    }
                }
                that.onLoad();
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        let isLogin = false;
        let userinfo = wx.getStorageSync('userInfo')
        that.setData({
            result:config.userInfo,
        });
        
        if (userinfo.phone != null) { isLogin = true; }
        that.setData({
            isLogin:isLogin
        })
        if(config.userInfo.phone!=null){
            this.getEquipmentInfo()
        }else{
            that.isLogin()
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
})